import {ref, toRefs} from "vue";
import useDict from "@/stores/dict";
import {getDictDatas} from "@/api/system/dict/data.ts";
import {SysDictDataRes} from "@/common/interface/dict/data.ts";

const initDict = (...args: any[]) => {
    const res: any = ref({});
    const dictStore = useDict();
    return (() =>{
        args.forEach((dictType: any) =>{
            res.value[dictType] = [];
            //判断当前字典类型是否存在
            const dicts = dictStore.getDict(dictType);
            if (dicts){
                res.value[dictType] = dicts;
            }else{
                //根据字典类型查询字典数据
                getDictDatas(dictType).then(response =>{
                    let data = response.data;
                    res.value[dictType] = data.map((d: SysDictDataRes) => {
                        return {
                            label: d.dictLabel,
                            value: d.dictValue,
                            dataType: d.listClass,
                            cssClass: d.cssClass,
                        }
                    })
                    dictStore.setDict(dictType, res.value[dictType]);
                })
            }
        })
        return toRefs(res.value);
    })();
}

export default initDict